// pages/detail-songs/index.js

import { rankingStore,playerStore} from '../../../store/index'
import {getallSongsMenu} from '../../../service/music-api'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    ranking:'',
    rankingInfo:{},
    type:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { 
   const type = options.type
   this.setData({type})
    if(type === "menu"){
      const id = options.id
      getallSongsMenu(id).then(res=>{
      this.setData({rankingInfo:res.playlist})
      })
    }else if(type === "rank"){
        //拿到传过来的名字
   const ranking = options.ranking
   this.setData({ranking})//保存到data
   rankingStore.onState(ranking,this.getRankingData)
    }
  },
  getRankingData:function(res){
      this.setData({rankingInfo:res})
  },
  handleSongItemClick(event){ //点击歌曲   
    //拿到点击歌曲的索引
    const index = event.currentTarget.dataset.index
    //把歌曲列表和index设置到player-store里
    playerStore.setState('playListSongs',this.data.rankingInfo.tracks)
    playerStore.setState('playListIndex',index)
    
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if(this.data.ranking){
    rankingStore.offState(this.data.ranking,this.getRankingData)
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})